<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>系统登录</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <script>
    tailwind.config = {
      theme: {
        extend: {
          colors: {
            primary: '#3b82f6',
            secondary: '#64748b',
            accent: '#0ea5e9',
            neutral: '#f1f5f9',
            'neutral-dark': '#1e293b',
          },
          fontFamily: {
            inter: ['Inter', 'system-ui', 'sans-serif'],
          },
        }
      }
    }
  </script>
  <style type="text/tailwindcss">
    @layer utilities {
      .content-auto {
        content-visibility: auto;
      }
      .animate-float {
        animation: float 6s ease-in-out infinite;
      }
      .animate-float-delay-1 {
        animation: float 6s ease-in-out 1s infinite;
      }
      .animate-float-delay-2 {
        animation: float 6s ease-in-out 2s infinite;
      }
      .card-shadow {
        box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.05), 0 8px 10px -6px rgba(0, 0, 0, 0.03);
      }
      .input-focus {
        @apply focus:ring-2 focus:ring-primary/50 focus:border-primary focus:outline-none;
      }
    }

    @keyframes float {
      0% { transform: translateY(0px); }
      50% { transform: translateY(-15px); }
      100% { transform: translateY(0px); }
    }
  </style>
</head>
<body class="bg-gradient-to-br from-blue-50 to-indigo-50 min-h-screen font-inter text-neutral-dark">
<!-- 背景装饰元素 -->
<div class="fixed inset-0 overflow-hidden pointer-events-none">
  <div class="absolute top-0 right-0 w-96 h-96 bg-primary/10 rounded-full -translate-y-1/2 translate-x-1/4 animate-float"></div>
  <div class="absolute bottom-0 left-0 w-72 h-72 bg-accent/10 rounded-full translate-y-1/2 -translate-x-1/4 animate-float-delay-1"></div>
  <div class="absolute top-1/3 left-1/4 w-40 h-40 bg-secondary/10 rounded-full animate-float-delay-2"></div>
</div>

<div class="relative min-h-screen flex items-center justify-center p-4">
  <!-- 登录卡片 -->
  <div class="w-full max-w-md bg-white rounded-2xl card-shadow overflow-hidden transform transition-all duration-300 hover:shadow-xl">
    <!-- 卡片顶部装饰 -->
    <div class="h-4 bg-gradient-to-r from-primary to-accent"></div>

    <!-- 登录表单 -->
    <div class="p-8">
      <div class="text-center mb-8">
        <div class="w-16 h-16 bg-primary/10 rounded-full flex items-center justify-center mx-auto mb-4">
          <i class="fa fa-lock text-primary text-2xl"></i>
        </div>
        <h2 class="text-[clamp(1.5rem,3vw,2rem)] font-bold text-neutral-dark">系统登录</h2>
        <p class="text-secondary mt-2">请输入您的账号和密码</p>
      </div>

      <form id="loginForm" class="space-y-5">
        <div class="space-y-2">
          <label for="url" class="block text-sm font-medium text-neutral-dark">服务器地址</label>
          <div class="relative">
                            <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-secondary">
                                <i class="fa fa-server"></i>
                            </span>
            <input type="text" id="url" name="url"
                   class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-200 bg-neutral input-focus transition-all duration-200"
                   placeholder="输入服务器地址" required>
          </div>
        </div>

        <div class="space-y-2">
          <label for="username" class="block text-sm font-medium text-neutral-dark">用户名</label>
          <div class="relative">
                            <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-secondary">
                                <i class="fa fa-user"></i>
                            </span>
            <input type="text" id="username" name="username"
                   class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-200 bg-neutral input-focus transition-all duration-200"
                   placeholder="输入用户名" required>
          </div>
        </div>

        <div class="space-y-2">
          <label for="password" class="block text-sm font-medium text-neutral-dark">密码</label>
          <div class="relative">
                            <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-secondary">
                                <i class="fa fa-key"></i>
                            </span>
            <input type="password" id="password" name="password"
                   class="w-full pl-10 pr-4 py-3 rounded-lg border border-gray-200 bg-neutral input-focus transition-all duration-200"
                   placeholder="输入密码" required>
            <button type="button" id="togglePassword" class="absolute inset-y-0 right-0 flex items-center pr-3 text-secondary hover:text-primary transition-colors">
              <i class="fa fa-eye-slash"></i>
            </button>
          </div>
        </div>

        <div class="flex items-center justify-between">
          <div class="flex items-center">
            <input type="checkbox" id="remember" name="remember" class="w-4 h-4 rounded border-gray-300 text-primary focus:ring-primary">
            <label for="remember" class="ml-2 text-sm text-secondary">记住密码</label>
          </div>
          <a href="#" class="text-sm text-primary hover:text-primary/80 transition-colors">忘记密码?</a>
        </div>

        <button type="submit" id="loginButton"
                class="w-full py-3 px-4 bg-primary hover:bg-primary/90 text-white font-medium rounded-lg transition-all duration-300 transform hover:translate-y-[-2px] hover:shadow-lg flex items-center justify-center">
          <span>登录系统</span>
          <i class="fa fa-arrow-right ml-2"></i>
        </button>
      </form>

      <div class="mt-8 text-center text-secondary text-sm">
        <p>还没有账号? <a href="#" class="text-primary hover:underline">立即注册</a></p>
      </div>
    </div>
  </div>
</div>

<script>
  // 切换密码可见性
  const togglePassword = document.getElementById('togglePassword');
  const passwordInput = document.getElementById('password');

  togglePassword.addEventListener('click', () => {
    const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
    passwordInput.setAttribute('type', type);
    togglePassword.querySelector('i').classList.toggle('fa-eye');
    togglePassword.querySelector('i').classList.toggle('fa-eye-slash');
  });

  // 登录表单提交处理
  const loginForm = document.getElementById('loginForm');
  const loginButton = document.getElementById('loginButton');

  loginForm.addEventListener('submit', async (e) => {
    e.preventDefault();

    // 获取表单数据
    const url = document.getElementById('url').value;
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;

    // 准备请求数据
    const requestData = {
      url: url,
      username: username,
      password: password
    };

    // 显示加载状态
    loginButton.disabled = true;
    loginButton.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i> 登录中...';

    try {
      // 发送登录请求
      const response = await fetch('/connection/getConnection', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(requestData)
      });

      // 检查HTTP状态码
      if (response.ok) {
        // 读取响应文本
        const responseText = await response.text();

        // 根据后端返回的字符串判断登录结果
        if (responseText === 'success') {
          alert('登录成功！即将跳转到主页...');
          window.location.href = '/html/table';
        } else if (responseText === 'error') {
          throw new Error('账号或密码错误，请重试');
        }
      } else {
        throw new Error('登录失败，请检查您的网络连接');
      }
    } catch (error) {
      // 显示错误信息
      alert(error.message);
    } finally {
      // 恢复按钮状态
      loginButton.disabled = false;
      loginButton.innerHTML = '<span>登录系统</span><i class="fa fa-arrow-right ml-2"></i>';
    }
  });
</script>
</body>
</html>
